class Solution
{
public:
    long long maximumSubsequenceCount(string text, string pattern)
    {
        if (pattern[0] == pattern[1])
        {
            long long count = 0;
            for (char c : text)
            {
                if (c == pattern[0])
                {
                    ++count;
                }
            }
            return (count + 1) * count / 2;
        }
        long long result = 0;
        long long p0Count = 0;
        long long p1Count = 0;
        for (char c : text)
        {
            if (c == pattern[0])
            {
                ++p0Count;
            }
            else if (c == pattern[1])
            {
                result += p0Count;
                ++p1Count;
            }
        }
        result += max(p0Count, p1Count);
        return result;
    }
};